﻿using ModernChannel.Data;
using ModernChannel.Model.Interface;
using ModernChannel.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModernChannel.Provider
{
    public class ShippingProvider : GenericProvider, IShippingProvider
    {
        public List<ShippingInfo> SearchShippingInfos(out int totalRecord, string textSearch, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ShippingInfo>();
            if (!string.IsNullOrWhiteSpace(textSearch))
                querry = querry.Where(x => x.ShiptoName.ToUpper().Contains(textSearch.ToUpper())
                                        || x.ShiptoFullname.ToUpper().Contains(textSearch.ToUpper())
                                        || x.ShiptoCode.ToUpper().Equals(textSearch.ToUpper())
                                        || x.Address1.ToUpper().Contains(textSearch.ToUpper())
                                        || x.Address2.ToUpper().Contains(textSearch.ToUpper())
                                        || x.Address3.ToUpper().Contains(textSearch.ToUpper())
                                        || x.Address4.ToUpper().Contains(textSearch.ToUpper())
                                        || x.BilltoAddress1.ToUpper().Contains(textSearch.ToUpper())
                                        || x.BilltoCode.ToUpper().Equals(textSearch.ToUpper())
                                        || x.BilltoName.ToUpper().Contains(textSearch.ToUpper())
                                        || x.BilltoFullname.ToUpper().Contains(textSearch.ToUpper())
                                        || x.City.ToUpper().Contains(textSearch.ToUpper())
                                        || x.Country.ToUpper().Contains(textSearch.ToUpper())
                                      );

            querry = querry.OrderBy(x => x.ShiptoCode);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }


        public List<ShippingInfo> SearchShippingInfos(out int totalRecord, string shipcode, string billcode, string shipname, string address, string country, string city, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<ShippingInfo>();
            if (!string.IsNullOrWhiteSpace(shipcode))
            {
                querry = querry.Where(x => x.ShiptoName.ToUpper().Contains(shipname.ToUpper()) || x.ShiptoFullname.ToUpper().Contains(shipname.ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(shipcode))
            {
                querry = querry.Where(x => x.ShiptoCode.ToUpper().Equals(shipcode.ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(address))
            {

                querry = querry.Where(x => x.Address1.ToUpper().Contains(address.ToUpper())
                                      || x.Address2.ToUpper().Contains(address.ToUpper())
                                      || x.Address3.ToUpper().Contains(address.ToUpper())
                                      || x.Address4.ToUpper().Contains(address.ToUpper())
                                      || x.BilltoAddress1.ToUpper().Contains(address.ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(billcode))
            {
                querry = querry.Where(x => x.BilltoCode.ToUpper().Equals(billcode.ToUpper()));
            }

            //  || x.BilltoName.ToUpper().Contains(textSearch.ToUpper())
            //  || x.BilltoFullname.ToUpper().Contains(textSearch.ToUpper())
            if (!string.IsNullOrWhiteSpace(city))
            {
                querry = querry.Where(x => x.City.ToUpper().Contains(city.ToUpper()));
            }

            if (!string.IsNullOrWhiteSpace(country))
            {
                querry = querry.Where(x => x.Country.ToUpper().Contains(country.ToUpper()));
            }

            querry = querry.OrderBy(x => x.ShiptoCode);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public ShippingInfo RetrieveShippingInfo(string shipcode)
        {
            var shipCode = shipcode ?? string.Empty;
            return repository.FindOne<ShippingInfo>(x => x.ShiptoCode.ToUpper() == shipCode);
        }
    }
}
